package com.car.postoffice;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map.Entry;

import com.car.framework.IDataMapping;

public class CMailHeader implements Serializable {
	private static final long serialVersionUID = 1L;

	private String mailType = "";
	private int mailId;
	private HashMap<String, String> parameters;
	private transient IDataMapping mapping;
	
	public CMailHeader(IDataMapping mapping){
		parameters = new HashMap<String, String>();
		this.setMapping(mapping);
	}
	
	public String getMailType() {
		if(mailType == null){
			return "";
		}
		return mailType;
	}
	public void setMailType(String mailType) {
		this.mailType = mailType;
	}
	public int getMailId() {
		return mailId;
	}
	public void setMailId(int mailId) {
		this.mailId = mailId;
	}
	
	public HashMap<String, String> getParameters() {
		return parameters;
	}
	public void setParameters(HashMap<String, String> parameters) {
		this.parameters = parameters;
	}
	public void setParameter(String key, String value){
		this.parameters.put(key, value);
	}
	
	public final IDataMapping getMapping() {
		return mapping;
	}
	
	public void setMapping(IDataMapping mapping){
		this.mapping = mapping;
	}
	
	@Override
	public boolean equals(Object obj){
		if(obj == null ||
		   this.getClass() != obj.getClass()){
			return false;
		}
		if(this == obj){
			return true;
		}
		CMailHeader header = (CMailHeader) obj;
		return  header.mailType.equals(this.mailType) &&
				header.mailId == this.mailId;
	}
	
	@Override
	public int hashCode(){
		return mailId * mailType.hashCode();
	}
	
	@Override
	public String toString(){
		StringBuffer buffer = new StringBuffer(128);
		buffer.append("[\n");
		buffer.append("  mailType=" + mailType + "\n");
		buffer.append("  mailId=" + mailId + "\n");
		buffer.append("  parameters:\n" +
					  "	 {\n");
		for(Entry<String, String> param : parameters.entrySet()){
			buffer.append("    "+ param.getKey() + ":" + param.getValue() + "\n");
		}
		buffer.append("  }\n");
		buffer.append("]\n");
		return buffer.toString();
	}
}
